package dao

import (
	"fawn/blog/consts"
	"fmt"

	"fawn/blog/models"

	"github.com/jinzhu/gorm"
	"go.uber.org/zap"
)

var TagsDao = newTagsDao()

type tagsDao struct{}

func newTagsDao() *tagsDao {
	return &tagsDao{}
}

func (d *tagsDao) TableName() string {
	return "t_tag"
}

// 获取标签列表
func (d *tagsDao) List(db *gorm.DB) (list []models.Tags, err error) {
	err = db.Table(d.TableName()).Order("id desc").Limit(consts.TagPageSize).Find(&list).Error
	if err != nil {
		l.Error("mysql",
			zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
			zap.String("error", err.Error()))
		return nil, err
	}

	return list, nil
}

// 获取标签
func (d *tagsDao) GetTagsInIds(db *gorm.DB, ids string) (t []models.Tags, err error) {
	err = db.Table(d.TableName()).Where("id in (?)", ids).Find(&t).Error
	if err != nil {
		l.Error("mysql",
			zap.String("sql", fmt.Sprintf("%+v", db.QueryExpr())),
			zap.String("error", err.Error()))
		return nil, err
	}

	return t, nil
}
